/**
 * 常用方法封装 请求，文件上传等
 * @author echo. 
 **/

const tui = {
	//接口地址
	imghost:'http://hanya.hjh30.cn/',
	website:'http://hanya.hjh30.cn/',
	apiUrl: function(path) {
		return 'http://hanya.hjh30.cn/'+path;
	},
	ajaxHeader:function(){
		return{
			'content-type': 'application/json',
			'X-Requested-With': 'XMLHttpRequest',
			'Authorization': tui.getToken()
		}
	},
	toast: function(text, duration, success) {
		uni.showToast({
			title: text || "出错啦~~",
			icon: success ? 'success' : 'none',
			duration: duration || 2000
		})
	},
	modal: function(title, content, showCancel, callback, confirmColor, confirmText) {
		uni.showModal({
			title: title||'提示',
			content: content,
			showCancel: showCancel,
			cancelColor: "#555",
			confirmColor: confirmColor || "#5677fc",
			confirmText: confirmText || "确定",
			success(res) {
				if (res.confirm) {
					callback && callback(true)
				} else {
					callback && callback(false)
				}
			}
		})
	},
	isAndroid: function() {
		const res = uni.getSystemInfoSync();
		return res.platform.toLocaleLowerCase() == "android"
	},
	isPhoneX: function() {
		const res = uni.getSystemInfoSync();
		let iphonex = false;
		let models=['iphonex','iphonexr','iphonexsmax','iphone11','iphone11pro','iphone11promax']
		const model=res.model.replace(/\s/g,"").toLowerCase()
		if (models.includes(model)) {
			iphonex = true;
		}
		return iphonex;
	},
	constNum: function() {
		let time = 0;
		// #ifdef APP-PLUS
		time = this.isAndroid() ? 300 : 0;
		// #endif
		return time
	},
	delayed: null,
	/**
	 * 请求数据处理
	 * @param string url 请求地址
	 * @param string method 请求方式
	 *  GET or POST
	 * @param {*} postData 请求参数
	 * @param bool isDelay 是否延迟显示loading
	 * @param bool isForm 数据格式
	 *  true: 'application/x-www-form-urlencoded'
	 *  false:'application/json'
	 * @param bool hideLoading 是否隐藏loading
	 *  true: 隐藏
	 *  false:显示
	 */
	request: function(url, method, postData, isDelay, isForm, hideLoading) {
		//接口请求
		let loadding = false;
		tui.delayed && uni.hideLoading();
		clearTimeout(tui.delayed);
		tui.delayed = null;
		if (!hideLoading) {
			tui.delayed = setTimeout(() => {
				uni.showLoading({
					mask: true,
					title: '请稍候...',
					success(res) {
						loadding = true
					}
				})
			}, isDelay ? 1000 : 0)
		}

		return new Promise((resolve, reject) => {
			uni.request({
				url: tui.apiUrl(url),
				data: postData,
				header: tui.ajaxHeader(),
				method: method?method:'GET', //'GET','POST'
				dataType: 'json',
				success: (res) => {
					clearTimeout(tui.delayed)
					tui.delayed = null;
					if (loadding && !hideLoading) {
						uni.hideLoading()
					}
					if (res.data && res.data.data == 'loginFail') {
						uni.clearStorageSync()
						tui.modal("提示","登录信息已失效，请重新登录", false, () => {
							//store.commit("logout") 登录页面执行
							uni.redirectTo({
								url: '/pages/login/login/login'
							})
						})
						return
					}
					resolve(res.data)
				},
				fail: (res) => {
					clearTimeout(tui.delayed)
					tui.delayed = null;
					tui.toast("网络不给力，请稍后再试~")
					reject(res)
				},
				complete:function(res){
					if(res.data.code ==0 && res.data.data == 'token_fail'){
						uni.clearStorage();
					}
				}
			})
		})
	},
	get:function(path,data){
		return this.request(path,'GET', data);
	},
	post:function(path,data){
		return this.request(path,'POST', data);
	},
	/**
	 * 上传文件
	 * @param string url 请求地址
	 * @param string src 文件路径
	 */
	uploadFile: function(url, src) {
		uni.showLoading({
			title: '请稍候...'
		})
		return new Promise((resolve, reject) => {
			const uploadTask = uni.uploadFile({
				url: tui.interfaceUrl() + url,
				filePath: src,
				name: 'imageFile',
				header: {
					'Authorization': tui.getToken()
				},
				formData: {
					// sizeArrayText:""
				},
				success: function(res) {
					uni.hideLoading()
					let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
					if (d.code % 100 == 0) {
						//返回图片地址
						let fileObj = d.data;
						resolve(fileObj)
					} else {
						that.toast(res.msg);
					}
				},
				fail: function(res) {
					reject(res)
					that.toast(res.msg);
				},

			})
		})
	},
	tuiJsonp: function(url, callback, callbackname) {
		// #ifdef H5
		window[callbackname] = callback;
		let tuiScript = document.createElement("script");
		tuiScript.src = url;
		tuiScript.type = "text/javascript";
		document.head.appendChild(tuiScript);
		document.head.removeChild(tuiScript);
		// #endif
	},
	//设置用户信息
	setUserInfo: function(userinfo){
		//uni.setStorageSync("thorui_token", token)
		uni.setStorageSync("userinfo", userinfo)
	},
	//获取token
	getToken() {
		return uni.getStorageSync("oauth_token")
	},
	//判断是否登录
	isLogin: function() {
		tui.get('user/api/chklogin').then(res=>{
			if(res.code===0){
				tui.modal('提醒',res.msg,false,function(){
					uni.navigateTo({
						url: '/pages/login/login/login'
					})
				});
			}
		})
		// return uni.getStorageSync("oauth_token") ? true : false
	},
	//跳转页面，校验登录状态
	openPage(url, isVerify) {
		// if (isVerify && !tui.isLogin()) {
		// 	uni.navigateTo({
		// 		url: '/pages/login/reg/reg'
		// 	})
		// } else {
		// 	uni.navigateTo({
		// 		url: url
		// 	});
		// }
		uni.navigateTo({
			url: url
		});
	},
	chkAuth(){
		tui.isLogin();
	},
	saveHistory(key){
		let keys= uni.getStorageSync('history_keyword');
		keys =keys? tui.strToJson(keys):[];//josn
		if(keys.includes(key)){
			return;
		}
		if(keys.length >9){
			keys.pop();
		}
		keys.unshift(key);
		keys = tui.jsonToStr(keys);
		uni.setStorageSync('history_keyword',keys);
	},
	getHistory(){
		let keys= uni.getStorageSync('history_keyword');
		return keys? tui.strToJson(keys):[];
	},
	clearHistory(){
		uni.setStorageSync('history_keyword','');
		return[];
	},
	jsonToStr :function(json){
        if(typeof json === 'object'){
            return JSON && JSON.stringify(json);
        }
    },
	strToJson : function(str){
        if(typeof str === 'string'){
            return JSON && JSON.parse(str);
        }
    }
}

export default tui
